package servlet;

import entity.Schedule;
import service.Impl.ScheduleServiceImpl;
import service.ScheduleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;

@WebServlet("/Schedule")
public class ScheduleServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method == null) {
            method = "findAll_schedule";
        }
        switch (method) {
            // 查询所有时间表信息
            case "findAll_schedule":
                ScheduleService scheduleService = new ScheduleServiceImpl();
                ArrayList<Schedule> scheduleList = null;
                scheduleList = scheduleService.getScheduleList();
                req.setAttribute("scheduleList", scheduleList);
                req.getRequestDispatcher("jsp/schedule.jsp").forward(req, resp);
                break;
            // 接受添加请求，跳转到添加时间表界面
            case "add":
                resp.sendRedirect("jsp/addSchedule.jsp");
                break;
            //添加
            case "addSchedule":
                Integer sid = Integer.valueOf(req.getParameter("scheduleId"));
                Integer cid = Integer.valueOf(req.getParameter("customerId"));
                Integer eid = Integer.valueOf(req.getParameter("employeeId"));
                Date start = Date.valueOf(req.getParameter("start"));
                Date end = Date.valueOf(req.getParameter("end"));
                Schedule schedule = new Schedule(sid,cid,eid,start,end);
                ScheduleService scheduleService1 = new ScheduleServiceImpl();
                //判断是否添加成功
                boolean isAdd = scheduleService1.addSchedule(schedule);
                // 如果成功添加，返回时间表列表界面
                if (isAdd) {
                    resp.sendRedirect("Schedule");
                }else { // 修改失败，跳转到错误界面
                    resp.sendRedirect("jsp/error.jsp");
                }
                break;
            // 接受更新请求，跳转到更改界面
            case "update":
                Integer scheduleId = Integer.valueOf(req.getParameter("scheduleId"));
                ScheduleService scheduleService2 = new ScheduleServiceImpl();
                Schedule schedule1 = scheduleService2.findById(scheduleId);
                req.setAttribute("schedule", schedule1);
                req.getRequestDispatcher("jsp/updateSchedule.jsp").forward(req,resp);
                break;
            // 修改某个时间表信息
            case "updateSchedule":
                // 获取时间表信息
                sid = Integer.valueOf(req.getParameter("scheduleId"));
                cid = Integer.valueOf(req.getParameter("customerId"));
                eid = Integer.valueOf(req.getParameter("employeeId"));
                start = Date.valueOf(req.getParameter("start"));
                end = Date.valueOf(req.getParameter("end"));
                Schedule schedule2 = new Schedule(sid,cid,eid,start,end);
                ScheduleService scheduleService3 = new ScheduleServiceImpl();
                // 判断是否修改成功
                boolean isUpdate = scheduleService3.updateSchedule(schedule2);
                // 如果成功修改，返回时间表列表界面
                if (isUpdate) {
                    resp.sendRedirect("Schedule");
                }else { // 修改失败，跳转到错误界面
                    resp.sendRedirect("jsp/error.jsp");
                }
                break;
            // 根据时间表编号删除时间表信息
            case "delete":
                Integer scheduleId1= Integer.valueOf(req.getParameter("scheduleId"));
                ScheduleService scheduleService4 = new ScheduleServiceImpl();
                boolean isDelete = scheduleService4.deleteSchedule(scheduleId1);
                if (isDelete) {
                    resp.sendRedirect("Schedule");
                } else {
                    resp.sendRedirect("jsp/error.jsp");
                }
                break;

            //审计
            case "sj":
                resp.sendRedirect("jsp/sj.jsp");
                break;

            default:
                break;
        }

    }

}
